﻿// -----------------------------------------------------------------------
// <copyright file="TimeSpanExtensions.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Helpers.Extensions
{
	#region Usings

	using System;
	using System.Globalization;
	using System.Text;

	#endregion

	/// <summary>
	/// Defines the extensions of the System.TimeSpan.
	/// </summary>
	public static class TimeSpanExtensions
	{
		#region Constants

		/// <summary>
		/// Represents the days in year.
		/// </summary>
		private const int DAYS_IN_YEAR = 365;

		/// <summary>
		/// Represents the days in month.
		/// </summary>
		private const int DAYS_IN_MONTH = 30;

		/// <summary>
		/// Represents the days in week.
		/// </summary>
		private const int DAYS_IN_WEEK = 7;

		/// <summary>
		/// Represents the hours in a day.
		/// </summary>
		private const int HOURS_IN_DAY = 24;

		/// <summary>
		/// Represents the minutes in hour.
		/// </summary>
		private const int MINUTES_IN_HOUR = 60;

		/// <summary>
		/// Represents the seconds in a minute.
		/// </summary>
		private const int SECONDS_IN_MINUTE = 60;

		private const int TOTAL_DISPLAYING_NUM = 3;

		#endregion

		#region Public Static Methods

		/// <summary>
		/// Gets the time interval in minutes/hours/days/weeks.
		/// </summary>
		/// <param name="interval">The time interval.</param>
		/// <returns>The string, which contains interval in minutes/hours/days/weeks.</returns>
		public static string GetTimeInterval(this TimeSpan interval)
		{
			int totalSeconds = (int)interval.TotalSeconds;

			int years = totalSeconds / SECONDS_IN_MINUTE / MINUTES_IN_HOUR / HOURS_IN_DAY / DAYS_IN_YEAR;

			totalSeconds = totalSeconds - (years * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * DAYS_IN_YEAR);

			int months = totalSeconds / SECONDS_IN_MINUTE / MINUTES_IN_HOUR / HOURS_IN_DAY / DAYS_IN_MONTH;

			totalSeconds = totalSeconds - (months * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * DAYS_IN_MONTH);

			int days = totalSeconds / SECONDS_IN_MINUTE / MINUTES_IN_HOUR / HOURS_IN_DAY;

			totalSeconds = totalSeconds - (days * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY);

			int hours = totalSeconds / SECONDS_IN_MINUTE / MINUTES_IN_HOUR;

			totalSeconds = totalSeconds - (hours * SECONDS_IN_MINUTE * MINUTES_IN_HOUR);

			int minutes = totalSeconds / SECONDS_IN_MINUTE;

			totalSeconds = totalSeconds - (minutes * SECONDS_IN_MINUTE);

			int seconds = totalSeconds;

			StringBuilder result = new StringBuilder();


			if (years > 0)
			{
				result
					.Append(FormatDate(years, "year"))
					.Append(FormatDate(months, "month"))
					.Append(FormatDate(days, "day"));
			}
			else if (months > 0)
			{
				result
					.Append(FormatDate(months, "month"))
					.Append(FormatDate(days, "day"))
					.Append(FormatDate(hours, "hour"));
			}
			else if (days > 0)
			{
				result
					.Append(FormatDate(days, "day"))
					.Append(FormatDate(hours, "hour"))
					.Append(FormatDate(minutes, "minute"));
			}
			else
			{
				result
					.Append(FormatDate(hours, "hour"))
					.Append(FormatDate(minutes, "minute"))
					.Append(FormatDate(seconds, "second"));
			}

			return result.ToString();
		}

		private static string FormatDate(int time, string alias)
		{
			string result = string.Empty;

			if (time > 0)
			{
				string postfix = (time > 1) ? "s" : string.Empty;

				result = string.Format("{0} {1}{2} ", time, alias, postfix);
			}

			return result;
		}

		#endregion
	}
}